package com.heima.ssm.dao;

import com.heima.ssm.pojo.Member;
import com.heima.ssm.pojo.Orders;
import com.heima.ssm.pojo.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Penguin
 * @create Date 2021/5/4  Time 10:01
 */
public interface IOrdersDao {
    /**
     * 因为orders-list页面显示涉及到商品，但不涉及member和traveller，所以需要列一下对product的查询。
     * 注意，订单与商品在这个业务逻辑里是1:1的关系，如果涉及到商城，就是1:n
     */
    @Select("select * from orders")
    //下面是以Orders表为基础，对Product的查询逻辑
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "orderNum",column = "orderNum"),
            @Result(property = "orderTime",column = "orderTime"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "peopleCount",column = "peopleCount"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "orderDesc",column = "orderDesc"),
            @Result(property = "product",column = "productId",javaType = Product.class,
                    one = @One(select = "com.heima.ssm.dao.IProductDao.findProductByProductIdFromOrders"))
    })
    List<Orders> findAll();


    /**
     * 订单详情页：从前端拿到指定订单id，传给后台。
     * 传来的id，首先要在orders表单进行查询，因为order表单包含了其他表单信息，所以需要联表查询
     * 如：订单对产品（1：1），订单对成员（1：1），订单对旅客（n:n）
     * n：n的本质就是1：n。需要注意的是：
     * 根据注解可知，联查时，首先对orders进行查询，在结果集resultS内，对其他表的字段名进行查询，
     * 如需要对产品表进行查询，而产品表的字段名在orders中是productId，将这个字段名传给IProductDao中，并作为参数，往sql语句里传
     *
     * 而n：n表，属性名是travellers，字段名应该根据orders的id去orders_traveller关系表单中查询，这是应该改注意的。
     * 方法的书写，最好见名知意。
     * 如findMemberByMemberIdWhereFromOrders，即：通过orders表单中的memberId查询member
     */
    @Select("select * from orders where id = #{ordersId}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "orderNum",column = "orderNum"),
            @Result(property = "orderTime",column = "orderTime"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "peopleCount",column = "peopleCount"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "orderDesc",column = "orderDesc"),
            @Result(property = "product",column = "productId",javaType = Product.class,
                    one = @One(select = "com.heima.ssm.dao.IProductDao.findProductByProductIdFromOrders")),
            @Result(property = "member",column = "memberId",javaType = Member.class,
                    one = @One(select = "com.heima.ssm.dao.IMemberDao.findMemberByMemberIdWhereFromOrders")),
            @Result(property = "travellers",column = "id",javaType = List.class,
                    many = @Many(select = "com.heima.ssm.dao.ITravellerDao.findTravellerByTravellerIdWhereFromOrders"))
    })
    Orders findOrdersByOrdersId(int ordersId);
}
